<%@ page language="java" import="java.util.*,table.*,table.node.*,java.net.*,jxl.write.*,jxl.*,java.io.*" pageEncoding="UTF-8"%>
<%
BaseNode rs = (BaseNode) request.getAttribute("results");

if(rs !=null){
  OutputStream  bos= null;
  bos=response.getOutputStream(); 

  if(bos != null){
    String tableName = rs.getNodeValue().toString();
    Date fileDate = new Date();
    
    response.setContentType("application/force-download;charset=utf-8");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-disposition","attachment;filename="+  new String(tableName.getBytes("utf-8"),"iso8859-1") +".xls");
    Label label = null;
    WritableWorkbook book = Workbook.createWorkbook(bos);
    WritableSheet sheet = book.createSheet(tableName,0);
    
    DataNode table = null;
    List<DataNode> cols = null;
    DataNode col = null;
    List<DataNode> cells = null;
    DataNode cell = null;
    
    table = rs;
    cols = rs.getSubNodes();
    int colOffset = 0;
    int rowOffset = 0;
    rowOffset++;
    for(int colIndex=0; colIndex < cols.size(); colIndex++){
    	 col = cols.get(colIndex);
       cells = col.getSubNodes();
       label = new Label(colIndex + colOffset, rowOffset -1, col.getNodeValue().toString());
       sheet.addCell(label);
       for(int rowIndex=0; rowIndex < cells.size(); rowIndex++){
         cell = cells.get(rowIndex);
         label = new Label(colIndex + colOffset,rowIndex + rowOffset
        		  ,cell.getNodeValue().toString());
         sheet.addCell(label);
       }
    }
    
    book.write(); 
    book.close();
  }
  if (bos != null) bos.close();
  out.clear();
  out = pageContext.pushBody();
}
%>